package com.yss.kettle.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.kettle.domain.JobTask;
import com.yss.kettle.service.IJobTaskService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 作业任务信息Controller
 *
 * @author yss
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/task" )
public class JobTaskController extends BaseController {
    @Autowired
    private IJobTaskService jobTaskService;

/**
 * 查询作业任务信息列表
 */
@RequiresPermissions("kettle:task:list" )
@GetMapping("/list" )
    public TableDataInfo list(JobTask jobTask) {
        startPage();
        List<JobTask> list = jobTaskService.selectJobTaskList(jobTask);
        return getDataTable(list);
    }

    /**
     * 导出作业任务信息列表
     */
    @RequiresPermissions("kettle:task:export" )
    @Log(title = "作业任务信息" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, JobTask jobTask) {
        List<JobTask> list = jobTaskService.selectJobTaskList(jobTask);
        EasyExeclUtil.export(response, list, JobTask.class );
    }

    /**
     * 获取作业任务信息详细信息
     */
    @RequiresPermissions("kettle:task:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return success(jobTaskService.getById(id));
    }

    /**
     * 新增作业任务信息
     */
    @RequiresPermissions("kettle:task:add" )
    @Log(title = "作业任务信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated JobTask jobTask) {
        jobTask.setId(IdUtils.fastSimpleUUID());
        jobTask.setCreateBy(SecurityUtils.getUsername());
        jobTask.setCreateTime(new Date());
        if (!jobTaskService.checkJobTaskNameUnique(jobTask)) {
            return error("名称【" + jobTask.getName() + "】的作业任务信息已存在");
        }
        return toAjax(jobTaskService.save(jobTask));
    }

    /**
     * 修改作业任务信息
     */
    @RequiresPermissions("kettle:task:edit" )
    @Log(title = "作业任务信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated JobTask jobTask) {
        jobTask.setUpdateTime(new Date());
        jobTask.setUpdateBy(SecurityUtils.getUsername());
        if (!jobTaskService.checkJobTaskNameUnique(jobTask)) {
            return error("名称【" + jobTask.getName() + "】的作业任务信息已存在");
        }
        return toAjax(jobTaskService.updateById(jobTask));
    }

    /**
     * 删除作业任务信息
     */
    @RequiresPermissions("kettle:task:remove" )
    @Log(title = "作业任务信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(jobTaskService.removeByIds(ids));
    }
}
